//package NetTrust;
import java.sql.*;
import java.util.Date;
import java.io.*;
import java.util.*;
import java.util.HashMap;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;

public class FriendsServlet extends HttpServlet{

	//Vector that adds the name of the broadcasters
	public static Vector vector_Buddyname =new Vector(20,5);
	public static int iNumBuddies=0;	
	//Initialize the servlet	
	public void init(ServletConfig config)
	{
	  System.out.println("FriendsServlet: init()");
	}
	
	public void doGet(HttpServletRequest request,HttpServletResponse response)    
		throws ServletException, IOException 
	{
		int ibuddyno;
		String strPrevNym=null;
		// set the response type
		response.setContentType("text/xml; charset=UTF-8"); 
		response.setHeader("Cache-Control","no-cache");
		String strNym=request.getParameter("nym");
		//String strNym="Home";
		if(strPrevNym!=strNym)
		{
			vector_Buddyname.clear();
			iNumBuddies=0;
			this.GetFriendsListNames(strNym);
			strPrevNym=strNym;
		}
		// obtain the writer to write the response
		PrintWriter out = response.getWriter();
		out.println("<?xml version=\"1.0\"?>");  
		out.println("<Buddy>");
		for( ibuddyno=0; ibuddyno < iNumBuddies; ibuddyno++ )
		{	
			out.println("<Buddyname>");
			out.println( vector_Buddyname.elementAt(ibuddyno));
			out.println("</Buddyname>");
		}
		out.println("</Buddy>");
	}
		
		
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// Connect()
	// Connects to the database
	// return value of type Connection
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static Connection Connect()
	{
		Connection conn = null;
		
		try
		{
			//Defines the Query statements
			//ResultSet rsBuddy,rsInternal,rsNymID;
			//Connect to the database
			String userName = "nettrust";
			String password = "nettrust06162006";
			String url= "jdbc:mysql://localhost/NetTrust";
			Class.forName ("com.mysql.jdbc.Driver").newInstance ();
			conn = DriverManager.getConnection (url, userName, password);
			System.out.println ("Database connection established");
		}
		catch( Exception e )
		{
			// Do nothing
			System.out.println("Connection failed");
		};  
		return( conn );
	}
	/* * * * * * * * * * * * * * * * * * * * * * * * * * *h * * */
	// end of function Connect()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// GetFriendsListNames
	// gets the name of the friends belonging to a particular nym
	// stores the list in the global vector of buddies.
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static void GetFriendsListNames(String strNymName)
	{
		ResultSet rsFriends;
		String strBuddyName;
		Connection conn=Connect();
		String strNymID=GetNymID(strNymName);
		try
		{
			Statement getBuddyName=conn.createStatement();
			
		      //Get the names of the broadcasters.
			rsFriends=getBuddyName.executeQuery("select NymName from nymnamemap,nyminfo where nyminfo.NymID='"+strNymID+"' and nymnamemap.NymID=BuddyID "); 
			while ( rsFriends.next() )
			{
				strBuddyName=rsFriends.getString("NymName");
				vector_Buddyname.addElement(strBuddyName);
				System.out.println("BuddyName:"+strBuddyName);
				iNumBuddies++;
			}	
			
		}
		catch(Exception e)
		{
			System.out.println(" The following Exception occured"+e);
		}
	}
	/* * * * * * * * * * * * * * * * * * * * * * * * * * *h * * */
	// end of function GetFriendListNames()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// GetNymID()
	// Gets the 128 bit NymID corresponding to a nym using the nym name as the parameter
	// returns 128 bit NymID
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	public static String GetNymID(String strNym)
	{
		Connection conn =Connect();
		String strNymid=null;
		ResultSet rsNymID;
		try
		{
			Statement getNymID= conn.createStatement();
		      //Get the nymid corresponding to the nym name from the database 
			rsNymID=getNymID.executeQuery("select NymID from nymnamemap where NymName='" + strNym + "'");
			while( rsNymID.next() )
			{
				strNymid=rsNymID.getString("NymID");
			}
		}
		catch( Exception e )
		{
			System.out.println ( "Database Exception is " + e );
		};  
		return(strNymid);
	}			
	/* * * * * * * * * * * * * * * * * * * * * * * * * * *h * * */
	// end of function GetNymID()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	
	public void destroy() {
		System.out.println("BroadCasterServlet: destroy()");
	}
}